{% extends "base.html" %}
{% block title %}定时任务{% endblock %}
{% load staticfiles %}
{% load custom_tags %}
{% block content %}

    <div class="am-modal am-modal-confirm" tabindex="-1" id="my-invalid">
        <div class="am-modal-dialog">
            <div class="am-modal-hd">HttpRunnerManager</div>
            <div class="am-modal-bd">
                亲，确定删除该定时任务么？
            </div>
            <div class="am-modal-footer">
                <span class="am-modal-btn" data-am-modal-cancel>取消</span>
                <span class="am-modal-btn" data-am-modal-confirm>确定</span>
            </div>
        </div>
    </div>

    <div class="admin-biaogelist">
        <div class="listbiaoti am-cf">
            <ul class="am-icon-flag on"> 任务管理</ul>
            <dl class="am-icon-home" style="float: right;"> 当前位置： 系统设置 > <a href="#">定时任务</a></dl>
            <dl>
                <button type="button" class="am-btn am-btn-danger am-round am-btn-xs am-icon-plus"
                        onclick="location='/api/add_task/'">新增任务
                </button>
            </dl>
        </div>

        <div class="am-btn-toolbars am-btn-toolbar am-kg am-cf">
            <form id="pro_filter" method="post" action="/api/periodictask/1/">
                <ul>
                    <li style="padding-top: 5px"><input value="{{ info.name }}" type="text" name="name"
                                                        class="am-input-sm am-input-xm"
                                                        placeholder="任务名称"/></li>
                    <li>
                        <button class="am-btn am-radius am-btn-xs am-btn-success"
                                style="margin-top: 9px;">查询
                        </button>
                    </li>
                </ul>
            </form>
        </div>
        <form class="am-form am-g" id="task_list" name="task_list" method="post" action="/api/run_batch_test/">
            <table width="100%" class="am-table am-table-bordered am-table-radius am-table-striped">
                <thead>
                <tr class="am-success">
                    <th class="table-check"><input type="checkbox" id="select_all"/></th>
                    <th class="table-title">INDEX</th>
                    <th class="table-set">ENABLED</th>
                    <th class="table-type">CRONTAB TIME</th>
                    <th class="table-title">TASK NAME</th>
                    <th class="table-title">KEYWORD ARGUMENTS</th>
                    <th class="table-date am-hide-sm-only">DATE_CHANGED</th>
                    <th width="163px" class="table-set">OPTIONS</th>
                </tr>
                </thead>
                <tbody>

                {% for foo in task %}
                    {% if foo.name != 'celery.backend_cleanup' %}
                        <tr>
                            <td><input type="checkbox" name="task_{{ foo.id }}" value="{{ foo.id }}"/></td>
                            <td>{{ forloop.counter }}</td>
                            {% if foo.enabled == True %}
                                <td class="am-hide-sm-only"><i class="am-icon-check am-text-warning"></i></td>
                            {% else %}
                                <td class="am-hide-sm-only"><i class="am-icon-close am-text-primary"></i></td>
                            {% endif %}
                            <td style="color: #3b87ee">{{ foo.description }}(m/h/d/dM/MY)</td>
                            <td>{{ foo.name }}</td>
                            <td>{{ foo.kwargs }}</td>
                            <td class="am-hide-sm-only">{{ foo.date_changed }}</td>
                            <td>
                                <div class="am-btn-toolbar">
                                    <div class="am-btn-group am-btn-group-xs">
                                        <button type="button"
                                                class="am-btn am-btn-default am-btn-xs am-text-secondary am-round"
                                                data-am-popover="{content: '开启', trigger: 'hover focus'}"
                                                onclick="task_set('{{ foo.name }}',true)"> <span
                                                class="am-icon-toggle-on"></span></button>
                                        <button type="button"
                                                class="am-btn am-btn-default am-btn-xs am-text-secondary am-round"
                                                data-am-popover="{content: '关闭', trigger: 'hover focus'}"
                                                onclick="task_set('{{ foo.name }}',false)"> <span
                                                class="am-icon-toggle-off"></span></button>
                                        <button type="button"
                                                class="am-btn am-btn-default am-btn-xs am-text-danger am-round"
                                                data-am-popover="{content: '删除', trigger: 'hover focus'}"
                                                onclick="invalid('{{ foo.name }}')"><span
                                                class="am-icon-trash-o"></span></button>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    {% endif %}
                {% endfor %}


                </tbody>
            </table>

            <div class="am-btn-group am-btn-group-xs">
                <button type="button" class="am-btn am-btn-default" onclick="location='/api/add_task/'"><span
                        class="am-icon-plus"></span> 新增
                </button>
            </div>

            <ul class="am-pagination am-fr">
                {{ page_list }}
            </ul>
            <hr/>
        </form>
    </div>
    <script type="text/javascript">

        function invalid(name) {
            $('#my-invalid').modal({
                relatedTarget: this,
                onConfirm: function () {
                    del_data_ajax(name, '/api/periodictask/1/')
                },
                onCancel: function () {
                }
            });
        }

        function task_set(id, mode) {
            var data = {
                "id": id,
                'mode': mode
            };
            $.ajax({
                type: 'post',
                url: '/api/periodictask/1/',
                data: JSON.stringify(data),
                contentType: "application/json",
                success: function (data) {
                    if (data !== 'ok') {
                        myAlert(data);
                    }
                    else {
                        window.location.reload();
                    }
                },
                error: function () {
                    myAlert('Sorry，服务器可能开小差啦, 请重试!');
                }
            });
        }

        $('#select_all').click(function () {
            var isChecked = $(this).prop("checked");
            $("input[name^='task']").prop("checked", isChecked);
        })

    </script>

{% endblock %}